import { reqGetApplyRefundList } from '../../api/shop';
Page({
  data: {
    refundOrders: [],
    loading: true
  },

  onLoad(options) {
    // 从本地存储中获取用户信息
    const userInfo = wx.getStorageSync('userinfo');
    if (!userInfo || !userInfo.userid) {
      wx.showToast({
        title: '未获取到用户信息',
        icon: 'none'
      });
      this.setData({ loading: false });
      return;
    }

    // 从用户信息中获取 userid
    const userId = userInfo.userid;
    this.fetchRefundOrders(userId);
  },

   // 获取退款订单
//    fetchRefundOrders(userId) {
//     this.setData({ loading: true });
    
//     reqGetApplyRefundList(userId)
//       .then(res => {
//         console.log("API 响应:", res);
//         if (res.code === 200) {
//           const orders = res.data.ordercakeList;
//           console.log("原始订单数据:", orders);
          
//           this.setData({
//             refundOrders: orders,
//             loading: false
//           });
//           console.log("refundOrders 数据结构:", this.data.refundOrders);
//         } else {
//           throw new Error(res.msgerr || '加载退款订单失败');
//         }
//       })
//       .catch(err => {
//         console.error('加载退款订单失败:', err);
//         wx.showToast({
//           title: err.message || '加载退款订单失败',
//           icon: 'none'
//         });
//         this.setData({ loading: false });
//       });
//   },
fetchRefundOrders(userId) {
    this.setData({ loading: true });
    reqGetApplyRefundList(userId)
     .then(res => {
        if (res.code === 200) {
          const orders = res.data.ordercakeList;
          orders.forEach(order => {
            if (order.shopreply) {
              const reg = /(1\d{10})/; 
              const match = order.shopreply.match(reg);
              if (match && match[1]) {
                const tel = match[1];
                // 拆分出 纯文本 和 电话号码
                order.shopreplyText = order.shopreply.replace(tel, ''); 
                order.shopreplyTel = tel; 
              } else {
                order.shopreplyText = order.shopreply;
                order.shopreplyTel = '';
              }
            }
          });
          this.setData({
            refundOrders: orders,
            loading: false
          });
        } else {
          throw new Error(res.msgerr || '加载退款订单失败');
        }
      })
     .catch(err => {
        console.error('加载退款订单失败:', err);
        wx.showToast({
          title: err.message || '加载退款订单失败',
          icon: 'none'
        });
        this.setData({ loading: false });
      });
  },
  // 日期格式化
  formatDate(dateString) {
    if (!dateString) return '';
    const date = new Date(dateString);
    return `${date.getFullYear()}-${(date.getMonth() + 1).toString().padStart(2, '0')}-${date.getDate().toString().padStart(2, '0')}`;
  },
  handleTap(e) {
    const tel = e.target.dataset.tel;
    if (tel) {
      wx.makePhoneCall({
        phoneNumber: tel 
      });
    }
  },
  // 检查是否在指定天数内
  isWithinDays(dateString, days) {
    if (!dateString) return false;
    const orderDate = new Date(dateString);
    const diffTime = Math.abs(new Date() - orderDate);
    const diffDays = Math.ceil(diffTime / (1000 * 60 * 60 * 24));
    return diffDays <= days;
  },

  // 获取状态文本
  getStatusText(status, refund) {
    const statusMap = {
      0: '待修改',
      1: '待配送',
      2: '待收货',
      3: '已完成'
    };
    
    if (refund === 1) return '退款中';
    if (refund === 2) return '已退款';
    if (refund === 3) return '不同意退款';
    return statusMap[status] || '未知状态';
  },
});